import { IpcEvent } from 'app/type'
import { dialog, BrowserWindow, ipcMain } from 'electron'
import { resourcesFile } from 'app/utils'

type MessageOption = [Omit<Electron.MessageBoxOptions, 'type'>, BrowserWindow?]

export class MessageBox {
    private static createMessageBoxShow(type: NonNullable<Electron.MessageBoxOptions['type']>) {
        const icon = resourcesFile('icon.png')
        // 这里将window参数反置，因为一般情况下其实不会用到window参数，这里参考了vscode的做法
        return function dialogShowMessageBox(
            options: Omit<Electron.MessageBoxOptions, 'type'>,
            window?: BrowserWindow,
        ) {
            if (window) {
                return dialog.showMessageBox(window, { type, ...options })
            }
            return dialog.showMessageBox({ type, ...options })
        }
    }

    static none = MessageBox.createMessageBoxShow('none')
    static info = MessageBox.createMessageBoxShow('info')
    static error = MessageBox.createMessageBoxShow('error')
    static question = MessageBox.createMessageBoxShow('question')
    static warning = MessageBox.createMessageBoxShow('warning')

    static init() {
        ipcMain.on(IpcEvent.MESSAGE_BOX_NONE, (e, ...args: MessageOption) => {
            return MessageBox.none.call(MessageBox.none, ...args)
        })
        ipcMain.on(IpcEvent.MESSAGE_BOX_INFO, (e, ...args: MessageOption) => {
            return MessageBox.info.call(MessageBox.info, ...args)
        })
        ipcMain.on(IpcEvent.MESSAGE_BOX_ERROR, (e, ...args: MessageOption) => {
            return MessageBox.error.call(MessageBox.error, ...args)
        })
        ipcMain.on(IpcEvent.MESSAGE_BOX_QUESTION, (e, ...args: MessageOption) => {
            return MessageBox.question.call(MessageBox.question, ...args)
        })
        ipcMain.on(IpcEvent.MESSAGE_BOX_WARNING, (e, ...args: MessageOption) => {
            return MessageBox.warning.call(MessageBox.warning, ...args)
        })
    }
}
